var KEY_TOKEN = 'CNMEDICINE_TOKEN' + CONTEXT_NAME;
// 此参数控制是否启用通过菜单切换标签页自动刷新页面，设置为true则会刷新（这种刷新方式不太合理，今后可将此处设置为false来禁用）
var __MM_REFRESH__ = false;


// 获取url中的参数
function getQueryVariable(variable){
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if(pair[0] == variable){return pair[1];}
	}
	return false;
}


/**
 * 保存持久化变量
 */
function global_setLocalStorage(key, value) {
    var curTime = new Date().getTime(); // 获取当前时间
    sessionStorage.setItem(key, JSON.stringify({
        val: value,
        time: curTime
    })); // 转换成json字符串序列
}

function global_removeLocalStorage(key) {
    sessionStorage.removeItem(key);
}

function global_cleanLocalStorage() {
    sessionStorage.clear()
}

/**
 * 取得持久化变量 exp是设置的过期时间
 */
function global_getLocalStorage(key, expire) {
    var val = sessionStorage.getItem(key); // 获取存储的元素
    if (!val) {
        return undefined;
    }

    var dataObj = JSON.parse(val); // 解析出json对象
    if (!dataObj) {
        return undefined;
    }

    if (!expire || new Date().getTime() - dataObj.time <= expire) {
        return dataObj.val;
    }
    if (new Date().getTime() - dataObj.time > exp) { // 如果当前时间-减去存储的元素在创建时候设置的时间
        return "TIME_OUT";
    }
}

function global_getAndRemoveLocalStorage(key, expire) {
    let val = global_getLocalStorage(key, expire);
    global_removeLocalStorage(key);
    return val;
}

$(document).ready(function() {
    $(document).ajaxSend(function(event, request, settings) {
        var TOKEN = global_getLocalStorage(KEY_TOKEN);
        if (TOKEN && TOKEN != 'TIME_OUT') {
            request.setRequestHeader("access-token", TOKEN);
        }
    });
    $(document).ajaxError(function(event, xhr, options, exc) {
        if (xhr.status == 403) {
            window.location = CONTEXT_NAME + '/index.html';
        }
    });
});

//监听行单击事件
$(document).on("click", ".layui-table-body table.layui-table tbody tr", function() {
    var index = $(this).attr('data-index');
    var tableBox = $(this).parents('.layui-table-box');
    //存在固定列
    if (tableBox.find(".layui-table-fixed.layui-table-fixed-l").length > 0) {
        tableDiv = tableBox.find(".layui-table-fixed.layui-table-fixed-l");
    } else {
        tableDiv = tableBox.find(".layui-table-body.layui-table-main");
    }
    var checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.laytable-cell-checkbox div.layui-form-checkbox I");
    if (checkCell.length > 0) {
        checkCell.click();
    }
});
//对td的单击事件进行拦截停止，防止事件冒泡再次触发tr的单击事件（Table的单击行事件不会拦截，依然有效）
$(document).on("click", "td div.laytable-cell-checkbox div.layui-form-checkbox", function(e) {
    e.stopPropagation();
});
$(document).on("click", "td input.layui-input-date", function(e) {
    e.stopPropagation();
});


//弹窗提示
//msg:string,弹窗提示信息,必须
//time:int,自动关闭所需毫秒,默认1000,0不关闭
//icon:int,提示图标，1成功，2错误，默认1
//area:array,弹窗宽高，默认350*200
//function alert_msg(msg, icon = 1, time = 1000, area = ['412px', '308px']) {
function alert_msg(msg, icon = 1, time = 1000) {
    if (icon == 1) {
        //layer.alert(msg, { icon: icon, time: time, area: area, title: ['提示'] });
        layer.alert(msg, { icon: icon, time: time, title: ['提示'] });
    } else {
        layer.alert(msg, { icon: icon, time: time, title: ['错误'] });
    }
}